package com.xmxsuperstar.app.ttac;

import java.io.File;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;

import com.xmxsuperstar.app.ttac.collections.FArrayList;
import com.xmxsuperstar.app.ttac.collections.FList;
import com.xmxsuperstar.app.ttac.handlers.HeadImageHandler;
import com.xmxsuperstar.app.ttac.handlers.TimeLineHandler;
import com.xmxsuperstar.app.ttac.thread.TimeLineMoreThread;
import com.xmxsuperstar.app.ttac.thread.TimeLineRefreshThread;
import com.xmxsuperstar.app.ttc.client.ClientConstants;
import com.xmxsuperstar.app.ttc.client.TwitterTimeLineClient;
import com.xmxsuperstar.app.ttc.client.model.TwitterStatus;

public abstract class TimeLineActivity extends TTACActivity {

	protected Handler headImageHandler = new HeadImageHandler();

	protected Handler timeLineHandler = new TimeLineHandler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ListView listView = new ListView(this);

		File cacheDir = this.getCacheDir();
		File stFile = new File(cacheDir, "st/" + getTimeLineDataFileName());
		FList<TwitterStatus> statusList = new FArrayList<TwitterStatus>(stFile);

		statusList.clear();

		StatusListAdapter statusListAdapter = new StatusListAdapter(statusList,
				this, headImageHandler);

		listView.setAdapter(statusListAdapter);

		TwitterTimeLineClient tc = getTC();
		Thread timeLineRefreshThread = new TimeLineRefreshThread(this,
				listView, this.headImageHandler, this.timeLineHandler, tc);
		timeLineRefreshThread.start();

		listView.setOnScrollListener(new StatusOnScrollListner(this, listView,
				headImageHandler, timeLineHandler));

		setContentView(listView);

	}

	protected abstract TwitterTimeLineClient getTC();

	protected abstract String getTimeLineDataFileName();

	class StatusOnScrollListner implements OnScrollListener {

		private final Context context;
		private final ListView listView;
		private final Handler headImageHandler;
		private final Handler timeLineHandler;

		private int firstVisibleItem;
		private int lastVisibleItem;
		private int totalItemCount;

		StatusOnScrollListner(Context context, ListView listView,
				Handler headImageHandler, Handler timeLineHandler) {
			this.context = context;
			this.listView = listView;
			this.headImageHandler = headImageHandler;
			this.timeLineHandler = timeLineHandler;
		}

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
			TwitterTimeLineClient tc = getTC();

			if (totalItemCount > 0
					&& firstVisibleItem == 0
					&& scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
				Thread timeLineRefreshThread = new TimeLineRefreshThread(
						context, listView, this.headImageHandler,
						this.timeLineHandler, tc);
				timeLineRefreshThread.start();
			}

			if (totalItemCount > 0
					&& lastVisibleItem == totalItemCount
					&& scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
				Thread timeLineMoreThread = new TimeLineMoreThread(context,
						listView, this.headImageHandler, this.timeLineHandler,
						tc);
				timeLineMoreThread.start();
			}

			Log.i(ClientConstants.LogTag, "totalItemCount=" + totalItemCount);
			Log.i(ClientConstants.LogTag, "lastVisibleItem=" + lastVisibleItem);
			Log.i(ClientConstants.LogTag, "firstVisibleItem="
					+ firstVisibleItem);
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			this.firstVisibleItem = firstVisibleItem;
			this.lastVisibleItem = firstVisibleItem + visibleItemCount;
			this.totalItemCount = totalItemCount;

			Log.i(ClientConstants.LogTag, "totalItemCount=" + totalItemCount);
			Log.i(ClientConstants.LogTag, "firstVisibleItem="
					+ firstVisibleItem);
			Log.i(ClientConstants.LogTag, "visibleItemCount="
					+ visibleItemCount);
		}

	}

}
